//
//  SearchViewController.m
//  jipin
//
//  Created by chuangjia on 23/5/15.
//  Copyright (c) 2015 liuchengbin. All rights reserved.
//

#import "SearchViewController.h"
#import "viewconfig.h"
#import "BaseModel.h"
#import "CellConfig.h"
#import "UMengShare.h"
#import "SearchConfig.h"
#import "WebViewController.h"
@interface SearchViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,SearchSegmentControlDelegate>
{
    
}
@property (nonatomic, strong) NSMutableArray *sectionArray;
@property (nonatomic, strong) NSMutableArray *historyArray;
@property (nonatomic, strong) NSMutableArray *hotArray;
@property (nonatomic, strong) NSMutableArray *searchArray;
@property (nonatomic, strong) NSMutableArray *searchResultArray;            // 存放搜索出结果的数组
@property (nonatomic, strong) UITextField *txtKey;              // 搜索控制器
@property (nonatomic, strong) UITableView* tabView;              // 搜索控制器
@property (nonatomic,strong) NSString *action;

@property(nonatomic,assign)int currentPage;
@property(nonatomic,assign)BOOL isLoadFinished;
@property (nonatomic,strong) SearchSegmentControl *pageController;
@property(nonatomic,strong)void(^actionBlock)(id,id);
@end

@implementation SearchViewController
@synthesize keyValue=_keyValue;
@synthesize action=_action;
@synthesize pageController=_pageController;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    [UMengShare event:CK_SEARCHHISTORY acc:1];
    if (!_sectionArray) {
        _sectionArray =[[NSMutableArray alloc] init];
    }
    self.sectionArray =[NSMutableArray arrayWithObjects:@"history",@"hot",@"search",@"searchresult", nil];
    
    if (!_historyArray) {
        _historyArray =[[NSMutableArray alloc] init];
    }
    if ([self.historyArray count]==0) {
        [self.sectionArray removeObject:@"history"];
    }
    if (!_hotArray) {
        _hotArray =[[NSMutableArray alloc] init];
    }
    if ([self.hotArray count]==0) {
        [self.sectionArray removeObject:@"hot"];
    }
    if (!_searchArray) {
        _searchArray=[[NSMutableArray alloc] init];
    }
    if ([self.searchArray count]==0) {
        [self.sectionArray removeObject:@"search"];
    }
    if (!_searchResultArray) {
        _searchResultArray=[[NSMutableArray alloc] init];
    }
    if ([self.searchResultArray count]==0) {
        [self.sectionArray removeObject:@"searchresult"];
    }
    _action=@"shaidan";
    _currentPage=0;
    _isLoadFinished = NO;
    [self initNAVUI];
    [self initTableView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
      DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)dealloc
{
    [_sectionArray removeAllObjects];
    _sectionArray=nil;
    [_historyArray removeAllObjects];
    _historyArray =nil;
    [_hotArray removeAllObjects];
    _hotArray =nil;
    [_searchResultArray removeAllObjects];
    _searchResultArray=nil;
    if (_pageController) {
        _pageController.delegate=nil;
        [_pageController removeFromSuperview];
        _pageController=nil;
    }
    if (_tabView) {
        _tabView.dataSource=nil;
        _tabView.delegate=nil;
        [_tabView removeFromSuperview];
        _tabView=nil;
    }
    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (NSMutableArray*)historyArray
{
    return [[SearchShare sharedInstance] getHistoryForSearch];
}
- (NSMutableArray*)hotArray
{
    return [ZShare sharedInstance].HotTagArray;
}

-(void)setSelectedCallBack:(void(^)(id,id))callback
{
    self.actionBlock=callback;
}
-(void)initNAVUI
{
    UIView *headerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, hightAuto(65))];
    headerView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:headerView];
    UIImageView *backView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, hightAuto(65))];
    backView.image =[UIImage imageNamed:@"titile_bg.png"];
    [headerView addSubview:backView];
    backView=nil;
    
    
    UIView *vSearchTxt = [[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,24,DEVICE_WIDTH -PHOTO_FRAME_WIDTH*6,34)];
    vSearchTxt.backgroundColor = UIColorFromRGB(0xefefef);
    vSearchTxt.layer.cornerRadius = 3;
    [headerView addSubview:vSearchTxt];
    UIImageView *imgSearch = [[UIImageView alloc] initWithFrame:CGRectMake((34-14)/2, (34-14)/2, 14, 14)];
    imgSearch.image = [UIImage imageNamed:@"search_icon"];
    [vSearchTxt addSubview:imgSearch];
    
    _txtKey = [[UITextField alloc] initWithFrame:CGRectMake(imgSearch.frame.origin.x+imgSearch.frame.size.width+6, 0, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*9, 34)];
    _txtKey.placeholder = @"搜索单品";
    _txtKey.font = FONT(13);
    _txtKey.clearButtonMode = UITextFieldViewModeWhileEditing;
    _txtKey.returnKeyType = UIReturnKeySearch;
    _txtKey.text =_keyValue;
    _txtKey.delegate = self;
    _txtKey.textColor =[UIColor darkGrayColor];
    _txtKey.enablesReturnKeyAutomatically = YES;
    [_txtKey addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [vSearchTxt addSubview:_txtKey];
    //
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame =CGRectMake(DEVICE_WIDTH-35-PHOTO_FRAME_WIDTH, 24, 40, 34);
    [rightBtn setTitle:@"取消" forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x808080) forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = FONT(15);
    [headerView addSubview:rightBtn];
    headerView=nil;
    [_txtKey becomeFirstResponder];
    
}
-(void)backAction:(id)sender
{
    self.actionBlock=nil;
    [_txtKey resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)initTableView
{
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, hightAuto(65), self.view.frame.size.width, self.view.frame.size.height-hightAuto(65)) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tabView];
    
    __weak typeof (self) wSelf = self;
    [_tabView addInfiniteScrollingWithActionHandler:^{
        if (wSelf.sectionArray.count==1 && [wSelf.sectionArray containsObject:@"searchresult"]) {
            if (wSelf.isLoadFinished) {
                //停止菊花
                [wSelf.tabView.infiniteScrollingView stopAnimating];
                [wSelf.tabView setShowsInfiniteScrolling:NO];
                return;
            }
            [wSelf.tabView.infiniteScrollingView startAnimating];
            if ([wSelf.searchResultArray count]>0) {
                wSelf.currentPage =(int)[wSelf.searchResultArray count]+1;
            }
            [wSelf searchResultWithKey:wSelf.txtKey.text];
        }else{
            [wSelf.tabView.infiniteScrollingView stopAnimating];
            [wSelf.tabView setShowsInfiniteScrolling:NO];
        }
    }];
    
    [_tabView.infiniteScrollingView stopAnimating];
    [_tabView setShowsInfiniteScrolling:NO];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)keyboardWillShow:(NSNotification*)notification //键盘出现
{
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _tabView.frame =CGRectMake(0, hightAuto(65), self.view.frame.size.width, self.view.frame.size.height-hightAuto(65)-_keyboardRect.size.height);
    
}
- (void)keyboardWillHide:(NSNotification*)notification //键盘下落
{
    _tabView.frame =CGRectMake(0, hightAuto(65), self.view.frame.size.width, self.view.frame.size.height-hightAuto(65));
}

#pragma mark ---tableView
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    _tabView .hidden = NO;
    self.tabView.tableFooterView =nil;
    if(textField.text.length>0)
    {
        //存历史
        [[SearchShare sharedInstance] saveHistoryForSearch:textField.text];
        self.currentPage=0;
        [_sectionArray removeAllObjects];
        [_searchArray removeAllObjects];
        [_searchResultArray removeAllObjects];
        [_tabView.infiniteScrollingView stopAnimating];
        [_tabView setShowsInfiniteScrolling:NO];
        self.tabView.tableFooterView =nil;
        [self.tabView reloadData];
        [ZShare showAnimationView:self.view center:CGPointZero];
        //刷新当前页面
        [self searchResultWithKey:textField.text];
    }
    return YES;
}
-(void)textFieldChanged:(UITextField *)textField
{
    DLog(@"textFieldChanged----%@",textField.text);
    _tabView.hidden = NO;
    self.tabView.tableFooterView =nil;
    if(textField.text.length>0)
    {
         self.sectionArray =[NSMutableArray arrayWithObjects:@"search", nil];
        [_tabView.infiniteScrollingView stopAnimating];
        [_tabView setShowsInfiniteScrolling:NO];
        [self searchDataWithKey:textField.text];
    }else
    {
        [self.sectionArray removeAllObjects];
         _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.sectionArray =[NSMutableArray arrayWithObjects:@"history",@"hot", nil];
        if ([self.historyArray count]==0) {
            [self.sectionArray removeObject:@"history"];
        }
        if ([self.hotArray count]==0) {
            [self.sectionArray removeObject:@"hot"];
        }
        [_searchArray removeAllObjects];
        [_searchResultArray removeAllObjects];
        [_tabView.infiniteScrollingView stopAnimating];
        [_tabView setShowsInfiniteScrolling:NO];
        [_tabView reloadData];
    }
}
#pragma mark - UITableViewDataSource Methods
#pragma mark 设置分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionArray.count;
}

#pragma mark 设置分组中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionArray objectAtIndex:section];
    if ([sectionTitle isEqualToString:@"history"]) {
        return 1;
    }else if ([sectionTitle isEqualToString:@"hot"]) {
        return 1;
    }else if ([sectionTitle isEqualToString:@"search"])
    {
        return self.searchArray.count;
    }else if ([sectionTitle isEqualToString:@"searchresult"])
    {
        return self.searchResultArray.count;
    }
    return 0;
}

#pragma mark 设置每个cell上显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
    NSString *sectionTitle =[self.sectionArray objectAtIndex:indexPath.section];
    if ([sectionTitle isEqualToString:@"history"])
    {
        SearchV1Cell *cell = (SearchV1Cell*)[tableView  dequeueReusableCellWithIdentifier:@"history"];
        if(cell == nil)
        {
            cell = [[SearchV1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"history"];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        [cell showInfo:self.historyArray key:@"history" indexPath:indexPath];
        return cell;
    }else if ([sectionTitle isEqualToString:@"hot"])
    {
        SearchV1Cell *cell = (SearchV1Cell*)[tableView  dequeueReusableCellWithIdentifier:@"SearchV1Cell"];
        if(cell == nil)
        {
            cell = [[SearchV1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchV1Cell"];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        [cell showInfo:self.hotArray key:@"hot" indexPath:indexPath];
        return cell;
    }else if ([sectionTitle isEqualToString:@"search"])
    {
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UIView *line =[[UIView alloc] initWithFrame:CGRectMake(0, 44, tableView.frame.size.width, 0.5)];
            line.backgroundColor=UIColorFromRGB(0xe5e5e5);
            [cell addSubview:line];
        }
        if (indexPath.row>=self.searchArray.count) {
            return cell;
        }
        NSString *str =[self.searchArray objectAtIndex:indexPath.row];
        cell.textLabel.text =str;
        return cell;
    }else if ([sectionTitle isEqualToString:@"searchresult"])
    {
        if ([self.action isEqualToString:@"shaidan"])
        {
            CategoryListCell *cell = (CategoryListCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[CategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.backgroundColor=[UIColor clearColor];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
                
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            if (indexPath.row>=self.searchResultArray.count) {
                return cell;
            }
            NSDictionary *dict =[self.searchResultArray objectAtIndex:indexPath.row];
            [cell showInfo:dict key:@"shaidan" indexPath:indexPath];
            return cell;
            
            
        }else if ([self.action isEqualToString:@"asking"])
        {
            TopicCell *cell = (TopicCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[TopicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.backgroundColor=[UIColor clearColor];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
                
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            if (indexPath.row>=self.searchResultArray.count) {
                return cell;
            }
            NSDictionary *dict =[self.searchResultArray objectAtIndex:indexPath.row];
            [cell showInfo:dict key:@"TopicCell" indexPath:indexPath];
            return cell;
        }
    }
    return cell;
}
#pragma mark 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *sectionTitle =[self.sectionArray objectAtIndex:indexPath.section];
    if ([sectionTitle isEqualToString:@"history"]) {
        return [SearchV1Cell returnCellHeight:self.historyArray];
    }else if ([sectionTitle isEqualToString:@"hot"]) {
        return [SearchV1Cell returnCellHeight:self.hotArray];
    }else if ([sectionTitle isEqualToString:@"search"])
    {
        return 44.0f;
    }else if ([sectionTitle isEqualToString:@"searchresult"])
    {
        if (indexPath.row>=self.searchResultArray.count) {
            return 0;
        }
        NSDictionary *dict =[self.searchResultArray objectAtIndex:indexPath.row];
        if ([self.action isEqualToString:@"shaidan"]) {
            
            return [CategoryListCell returnCellHeight:dict];
        }else if ([self.action isEqualToString:@"asking"])
        {
            return [TopicCell returnCellHeight:dict];
        }
        return 0;
    }
    return 44.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionArray objectAtIndex:section];
    if ([sectionTitle isEqualToString:@"history"]) {
        return  0.01;
    }else if ([sectionTitle isEqualToString:@"hot"]) {
        return  PHOTO_FRAME_WIDTH*3;
    }else if ([sectionTitle isEqualToString:@"searchresult"])
    {
         return  PHOTO_FRAME_WIDTH*4;
    }
    return 0.01;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    NSString *sectionTitle =[self.sectionArray objectAtIndex:section];
    
    if ([sectionTitle isEqualToString:@"history"])
    {
        return nil;
    }else if ([sectionTitle isEqualToString:@"hot"])
    {
        UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, PHOTO_FRAME_WIDTH*3)];
        sectionView.backgroundColor =[UIColor whiteColor];
        UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH, tableView.frame.size.width-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*2)];
        tagValue.backgroundColor = [UIColor clearColor];
        tagValue.textColor = UIColorFromRGB(0xcccccc);
        tagValue.textAlignment = NSTextAlignmentLeft;
        tagValue.font = FONT(14);
        tagValue.text =@"热门搜索";
        [sectionView addSubview:tagValue];
        tagValue=nil;
        return sectionView;
    }else if ([sectionTitle isEqualToString:@"search"])
    {
        return nil;
    }else if ([sectionTitle isEqualToString:@"searchresult"])
    {
        UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, PHOTO_FRAME_WIDTH*4)];
        sectionView.backgroundColor =[UIColor whiteColor];
        if (!_pageController) {
            _pageController =[[SearchSegmentControl alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, PHOTO_FRAME_WIDTH*4)];
            _pageController.backgroundColor = [UIColor clearColor];
            _pageController.delegate=self;
            [_pageController AddSegumentArray:@[@"晒单",@"话题"]];
        }
        [sectionView addSubview:_pageController];
        return sectionView;
    }
    return nil;
   
}
-(void)uisegumentSelectionChange:(NSInteger)selection
{
    if (selection==0) {
         self.action=@"shaidan";
    }else{
         self.action=@"asking";
    }
    self.currentPage=0;
    [self.searchResultArray removeAllObjects];
    self.tabView.tableFooterView =nil;
    [self.tabView reloadData];
    [ZShare showAnimationView:self.view center:CGPointZero];
    [self searchResultWithKey:_txtKey.text];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        [_txtKey resignFirstResponder];
        NSString *str =@"";
        NSString *sectionTitle =[self.sectionArray objectAtIndex:indexPath.section];
        if ([sectionTitle isEqualToString:@"search"])
        {
            str =[_searchArray objectAtIndex:indexPath.row];
            //存历史
            [[SearchShare sharedInstance]  saveHistoryForSearch:str];
            [_txtKey resignFirstResponder];
            _txtKey.text = str;
            //刷新当前页面
            [self searchResultWithKey:str];
        }else if ([sectionTitle isEqualToString:@"searchresult"])
        {
            if (indexPath.row>=self.searchResultArray.count) {
                return;
            }
            NSDictionary *dict =[self.searchResultArray objectAtIndex:indexPath.row];
            if ([self.action isEqualToString:@"shaidan"]) {
                ShaiorderDetailViewController *good =[[ShaiorderDetailViewController alloc] init];
                good.type =-1;
                good.dictionary =dict;
                good.photo_id = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                [self.navigationController pushViewController:good animated:YES];
            }else if ([self.action isEqualToString:@"asking"])
            {
                
                int is_shaidan =[[dict objectForKey:@"is_shaidan"] intValue];
                if (is_shaidan==1)
                {
                    ShaiorderDetailViewController *good =[[ShaiorderDetailViewController alloc] init];
                    good.type =-1;
                    good.dictionary = dict;
                    good.photo_id = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                    [self.navigationController pushViewController:good animated:YES];
                    good=nil;
                }else if (is_shaidan==20) {
                    WebViewController *web =[[WebViewController alloc] init];
                    [self.navigationController pushViewController:web animated:YES];
                    web=nil;
                    
                }else{
                        GoodViewController *good =[[GoodViewController alloc] init];
                        good.type =-1;
                        good.dictionary =dict;
                        good.photo_id = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                        [self.navigationController pushViewController:good animated:YES];
                        good =nil;
                }
            }
        }

}

-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath *)indexPath
{
    
    switch (type) {
        case BEventType_tag:
        {
            if([obj isEqualToString:@"清除搜索历史"])
            {
                [[SearchShare sharedInstance] clearHistoryForSearch];
                [self.tabView reloadData];
            }else
            {
                //存历史
                [[SearchShare sharedInstance]  saveHistoryForSearch:obj];
                [_txtKey resignFirstResponder];
                _txtKey.text = obj;
                [self searchResultWithKey:obj];
            }
          
        }
            break;
            
        default:
            break;
    }
}
////自动提醒关键词
-(void)searchDataWithKey:(NSString *)key
{
    if (key==nil||key.length==0) {
        key=@"";
    }
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:key forKey:@"word"];
    [dict setValue:@"search" forKey:@"action"];
    [dict setValue:@"" forKey:@"cate_id"];
    [dict setValue:@"" forKey:@"brand_id"];
    __weak typeof (self) wSelf = self;
    [BaseModel suggestion:dict success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        [wSelf parserResultObject:result];
    } failure:^(NSError *error) {
        [wSelf.searchArray removeAllObjects];
        [wSelf.tabView.infiniteScrollingView stopAnimating];
        [wSelf.tabView setShowsInfiniteScrolling:NO];
        [wSelf.tabView reloadData];
        [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
-(void)parserResultObject:(id)resultObject
{
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSArray class]]) {
        self.searchArray =[NSMutableArray arrayWithArray:resultObject];
    }
    [_tabView reloadData];
    
}
////搜索结果数据
-(void)searchResultWithKey:(NSString *)key
{
    if (key==nil||key.length==0) {
        key=@"";
    }
    
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:key forKey:@"word"];
    [dict setValue:self.action forKey:@"action"];
    [dict setValue:[NSString stringWithFormat:@"%d",self.currentPage] forKey:@"from"];
    __weak typeof (self) wSelf = self;
    [BaseModel searchlist:dict success:^(AFHTTPRequestOperation *operation, id result) {
      [wSelf parserSearchResultObject:result];
        [ZShare hiddenAnimationView:wSelf.view text:@""];
    } failure:^(NSError *error) {
        [wSelf.searchResultArray removeAllObjects];
        [wSelf.tabView.infiniteScrollingView stopAnimating];
        [wSelf.tabView setShowsInfiniteScrolling:NO];
        [wSelf.tabView reloadData];
        [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
-(void)parserSearchResultObject:(id)resultObject
{
    if (resultObject==nil) {
        [self.sectionArray removeAllObjects];
        self.sectionArray =[NSMutableArray arrayWithObjects:@"history",@"hot", nil];
        if ([self.historyArray count]==0) {
            [self.sectionArray removeObject:@"history"];
        }
        if ([self.hotArray count]==0) {
            [self.sectionArray removeObject:@"hot"];
        }
        [_tabView reloadData];
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        id data = [resultObject objectForKey:@"data"];
        if (data && [data isKindOfClass:[NSArray class]]) {
            if (self.currentPage==0) {
                [self.sectionArray removeAllObjects];
                [self.searchResultArray removeAllObjects];
            }
            for (id obj in data) {
                [self.searchResultArray addObject:obj];
            }
            if (self.searchResultArray.count>0) {
                if (![self.sectionArray containsObject:@"searchresult"]) {
                    [self.sectionArray removeAllObjects];
                    self.sectionArray =[NSMutableArray arrayWithObjects:@"searchresult", nil];
                    [self.tabView setShowsInfiniteScrolling:YES];
                }
            }
        }
        //end
        self.isLoadFinished =  [[resultObject objectForKey:@"end"] boolValue];
        
        if (self.isLoadFinished) {
            [self.tabView.infiniteScrollingView stopAnimating];
            [self.tabView setShowsInfiniteScrolling:NO];
            NSString *end_msg =[resultObject objectForKey:@"end_msg"];
            self.tabView.tableFooterView =[ZShare footerView:end_msg];
        }
        id no_data =  [resultObject objectForKey:@"no_data"];
        if (no_data&&[no_data isKindOfClass:[NSString class]]) {
            [self.sectionArray removeAllObjects];
            self.sectionArray =[NSMutableArray arrayWithObjects:@"searchresult", nil];
            [ZShare hiddenAnimationView:self.view text:no_data];
            self.tabView.tableFooterView =[self defaultView:no_data];//[CommonViewController footerView:no_data];
        }else {
            [ZShare hiddenAnimationView:self.view];
        }
    }
  
    [self.tabView reloadData];
    
}
//无数据展示样式
-(UIView *)defaultView:(NSString *)title
{
    
    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height-100)];
    view.backgroundColor=[UIColor clearColor];
    view.tag =100000;

    UILabel* Infos = [[UILabel alloc]initWithFrame:CGRectMake(0, view.frame.origin.y+35, view.frame.size.width, 20)];
    Infos.backgroundColor = [UIColor clearColor];
    Infos.textAlignment = NSTextAlignmentCenter;
    Infos.textColor = UIColorFromRGB(0xcccccc);
    Infos.font = FONT(14);
    Infos.text =title;
    [view addSubview:Infos];
    

    return view;
}

@end
